package com.sumainfor.pc.gmio.controller.assistant;

import cn.afterturn.easypoi.entity.vo.MapExcelConstants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiniu.util.Json;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ConvertDateTime;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.gmio.service.impl.StatementServiceImpl;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Jack Chen
 * @Create 2019-01-23 18:03
 * 报表导出
 */
@Controller
@RequestMapping("/statementOutput")
public class StatementController {

    @Autowired
    private StatementServiceImpl service;


    /**
     * 获取杂费收款表格数据
     *
     * @param page
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("/getIncidentalsForGetTable")
    public JsonResult getIncidentalsForGet(Page page, @RequestParam Map<String, Object> params) {
        return JsonResult.success(service.getIncidentalsForGet(page, params));
    }

    @RequestMapping("/exportIncidentalsForGet")
    public String exportIncidentalsForGet(ModelMap modelMap, @RequestParam Map<String, Object> params) throws Exception {
        service.exportIncidentalsForGet(modelMap, params);
        return MapExcelConstants.EASYPOI_MAP_EXCEL_VIEW;
    }

    /**
     * 获取杂费收款数据表格
     */
    @ResponseBody
    @RequestMapping("/getIncidentalsForGetTabless")
    public JsonResult getIncidentalsForGetTabless(@RequestParam Map<String,Object> params, Pager pager)throws Exception{


        Map<String,Object> dealMap = this.DateDealwith(params);
        //如果为空那么取当年的数据范围
        params.put("startTime", dealMap.get("StartTime"));
        params.put("endTime", dealMap.get("EndTime"));

        pager.setPageno(Integer.parseInt(params.get("pageno").toString()));
        pager.setPagesize(Integer.parseInt(params.get("pagesize").toString()));
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        Integer cout =  service.getIncidentalsForGetTablessCount(params);
        System.out.println("cout"+cout);
        if(cout == 0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(),params,cout);
        }else {
            List<Map<String, Object>> getTenHousList = service.getIncidentalsForGetTabless(params);
            return pageUtils.getJsonResult(getTenHousList,params,cout);
        }

    }


    /**
     * 获取杂费收款数据表格
     */
    @ResponseBody
    @RequestMapping("/getIncidentalsForOutss")
    public JsonResult getIncidentalsForOutss(@RequestParam Map<String,Object> params, Pager pager)throws Exception{

        Map<String,Object> dealMap = this.DateDealwith(params);
        //如果为空那么取当年的数据范围
        params.put("startTime", dealMap.get("StartTime"));
        params.put("endTime", dealMap.get("EndTime"));

        pager.setPageno(Integer.parseInt(params.get("pageno").toString()));
        pager.setPagesize(Integer.parseInt(params.get("pagesize").toString()));
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        Integer cout =  service.getIncidentalsForOutssCount(params);
        System.out.println("cout"+cout);
        if(cout == 0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(),params,cout);
        }else {
            List<Map<String, Object>> getTenHousList = service.getIncidentalsForOutss(params);
            return pageUtils.getJsonResult(getTenHousList,params,cout);
        }

    }




    /**
     * 功能描述:
     * 时间查询处理
     * 月份定义：上月25日00：00到本月24日00:00
     * 前端。传递2019-01至2019-05
     * 转换之后。应该查询的数据是2019-01-25-2019-05-24之间的数据
     *
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/20 15:14
     */
    public Map<String, Object> DateDealwith(Map<String, Object> params)throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        //月份开始时间
        String monthStartTime;
        //月份结束时间
        String monthEndTime;
        //如果开始月份为空，默认当年1月
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"startTime"})){
            DateTime date = ConvertDateTime.dateFormatStrToTime("yyyy-MM",String.valueOf(ConvertDateTime.getCurrentDateElement(1))+"-01");
            monthStartTime= date.toString("yyyy-MM");
        }else{
            DateTime date =ConvertDateTime.dateFormatStrToTime("yyyy-MM",params.get("startTime").toString());
            monthStartTime= date.toString("yyyy-MM");
        }
        //如果结束月份为空，默认当年12月
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"endTime"})){
            DateTime date = ConvertDateTime.dateFormatStrToTime("yyyy-MM",String.valueOf(ConvertDateTime.getCurrentDateElement(1))+"-12");
            monthEndTime=date.toString("yyyy-MM");
        }else{
            DateTime date =ConvertDateTime.dateFormatStrToTime("yyyy-MM",params.get("endTime").toString());
            monthEndTime=date.toString("yyyy-MM");
        }

        result.put("StartTime", monthStartTime + "-26");
        result.put("EndTime", monthEndTime + "-25");
        return result;
    }













    /**
     * 获取杂费付款表格数据
     *
     * @param page
     * @param params
     * @return
     */
    @ResponseBody
    @RequestMapping("/getIncidentalsForOut")
    public JsonResult getIncidentalsForOut(Page page, @RequestParam Map<String, Object> params) {
        return JsonResult.success(service.getIncidentalsForOut(page, params));
    }

    /**
     * 导出Excel表格 付款
     *
     * @param params
     */
    @RequestMapping("/exportIncidentalsForOut")
    public String exportIncidentalsForOut(ModelMap modelMap, @RequestParam Map<String, Object> params) throws Exception {
        service.exportIncidentalsForOut(modelMap, params);
        return MapExcelConstants.EASYPOI_MAP_EXCEL_VIEW;
    }


    /**
     * 装修明细报表
     *
     * @param page
     * @param params
     * @return
     */
    @RequestMapping("/getPlanDetailTable")
    @ResponseBody
    public JsonResult getPlanDetailTable(Page page, @RequestParam Map<String, Object> params) {
        return service.getPlanDetailTable(page, params);
    }

    /**
     * 导出装修账单表格
     *
     * @param modelMap
     * @param params
     * @return
     */
    @RequestMapping("/exportPlanDetailExcel")
    public String exportPlanDetailExcel(ModelMap modelMap, @RequestParam Map<String, Object> params) {
        service.exportPlanDetailExcel(modelMap, params);
        return MapExcelConstants.EASYPOI_MAP_EXCEL_VIEW;
    }

}
